package com.stock.stock.modules.sys.controller;


import com.stock.stock.modules.sys.entity.SysUser;
import com.stock.stock.utils.JacksonUtil;
import com.stock.stock.utils.UserTokenManager;
import com.stock.stock.utils.R;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * ${comments}
 *
 * @author abc
 * @email 635177408@163.com
 * @date 2020-05-21 22:20:38
 */
@RestController
public class LoginController {
    @PostMapping(value = "/user/login")
    public Object login(@RequestBody String body) {
        String username = JacksonUtil.parseString(body, "username");
        String password = JacksonUtil.parseString(body, "password");

        HashMap<String, Object> data = new HashMap<>();

        if (username != null && password != null && username.equals("admin") && password.equals("admin123")) {
            String token = UserTokenManager.generateToken(1);
            data.put("token", token);
            return R.ok().put("data", data);
        } else {
            return R.error("用户名或者密码不对");
        }
    }

    @GetMapping(value = "/user/info")
    public Object info() {
        HashMap<String, Object> data = new HashMap<>();
        data.put("roles", "admin");
        data.put("name", "Super admin");
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return R.ok().put("data", data);
    }

}
